package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import java.io.File;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * Created by Jinni on 2017/8/22 0022.
 */

public class CompanyIdentificationModel implements CompanyIdentificationModelImpl {

    private static final String             TAG = "CompanyIdentificationMo";
    private OnRequestListener               mListener;

    public CompanyIdentificationModel(OnRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getSubmitIdentificationResult(String uid, String name, String title, String tel, List<File> images) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        for (File image : images) {
            builder.addFormDataPart("picname[]", image.getName(), RequestBody.create(MediaType.parse("image/*"), image));
        }
        builder.addFormDataPart("name", name)
                .addFormDataPart("uid", uid)
                .addFormDataPart("tel", tel)
                .addFormDataPart("title", title);
        RequestBody requestBody = builder.build();
        String url = HttpRequestConstant.SUBMIT_CP_IDENTIFICATION;
        Observable<ResponseBody> loginResult = DataManager.requestIdentificationWithBody(
                url,
                requestBody
        );
        Observable<ResponseBody> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<ResponseBody>() {

            @Override
            public void onNext(@NonNull ResponseBody bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestListener {
        void onSuccess(ResponseBody bean);

        void onError(Throwable throwable);
    }
}
